console.log('新闻资讯')
var title = '公司新闻'
var content = document.querySelector('.content')
var company = document.querySelector('#company')
// var trade = document.querySelector('#trade')
var read = document.querySelector('.read')
var details = document.querySelector('.details')
var read2 = document.querySelector('.read2')
var content_r = document.querySelector('.content_r')
var content2 = document.querySelector('.content2')
var details2 = document.querySelector('.details2')
var content2_company = document.querySelector('.content2_company')
// company.addEventListener('click', function(){
//     console.log('公司新闻')
//     title = this.innerHTML
//     console.log(title)
//     this.className = 'clickStyle'
// })

// trade.addEventListener('click', function(){
//     console.log('行业新闻')
//     this.className = 'clickStyle'
//     company.className = ''
// })
// 详情一
// read.addEventListener('click', function(){
//     content.style.display = 'none'
//     details.style.display = 'block'
//     details2.style.display = 'none'
//     content2.style.display = 'block'
//     document.body.scrollTop = 0
//     document.documentElement.scrollTop = 0
// })
// content2_company.addEventListener('click', function(){
//     content.style.display = 'block'
//     content2.style.display = 'none'
// })
// // 详情二
// read2.addEventListener('click', function(){
//     content.style.display = 'none'
//     details.style.display = 'none'
//     details2.style.display = 'block'
//     content2.style.display = 'block'
//     document.body.scrollTop = 0
//     document.documentElement.scrollTop = 0
// })
// 立即试用按钮
// var away = document.querySelector('.away')
// away.addEventListener('click', function () {
//     window.open = 'https://landlord.zhihuifangdong.net/#/login'
// })
var pagesCurrent = 1;
var lastPages = '';
window.onload = getData(1);

function getData(nowCurrent) {
    pagesCurrent = Number(nowCurrent);
    $.ajax({
        url: `https://api.zhihuifangdong.net/admin/pNews/getAllNews?current=${nowCurrent}&size=10&status=1`,
        success: function (result) {
            // $("#div1").html(result);
            console.log(result.data.records);
            let allData = '';
            if (result.data.records.length != 0) {
                for (let k = 0; k < result.data.records.length; k++) {
                    let nowData = result.data.records[k];
                    // 过滤以@开头以:结尾的文本
                    let regExp = /\<img.*?\>/g;
                    nowData.content = nowData.content.replace(regExp, "");
                    console.log('过滤图片', nowData.content.replace(regExp, ""))
                    let dh = '';
                    dh += '<div class="mainCenter">\n' +
                        '            <div class="imgContainer">\n' +
                        '                <img src="' + nowData.cover + '" alt="">\n' +
                        '            </div>\n' +
                        '            <div class="textContainer">\n' +
                        '                <div>\n' +
                        '                    <div class="textTitle">' + nowData.title + '</div>\n' +
                        '                    <div class="textDate">' + nowData.gmtCreate.split(' ')[0] + '</div>\n' +
                        '                </div>\n' +
                        '                <div class="mainText threeRow">\n' + nowData.content +
                        '                </div>\n' +
                        '                <div class="checkMore" id="' + nowData.id + '" onclick="checkMore(this)">查看更多</div>\n' +
                        '            </div>\n' +
                        '        </div>';
                    allData += dh;
                }

            } else {
                allData = '    <div class="emptyInfo">\n' +
                    '        暂无新闻内容，敬请期待！\n' +
                    '    </div>'
            }
            document.getElementById('news-content').innerHTML = allData;
            let pages = '';
            lastPages = result.data.pages;
            for (let k = 0; k < Number(result.data.pages); k++) {
                pages += `<div class="pageItem ${Number(k + 1) === Number(nowCurrent) ? 'atcivePage' : ''}" onclick="goTopage(this)">${(k + 1)}</div>`

            }
            console.log(pages);
            document.getElementById('changePage').innerHTML = '<div class="pageItem turnItem" onclick="turnPage(false)">上一页</div>\n' +
                pages +
                '        <div class="pageItem turnItem" onclick="turnPage(true)">下一页</div>';
            document.body.scrollTop = document.documentElement.scrollTop = 0;
        }
    });
}

function turnPage(turn) {
    if (turn) {
        if (pagesCurrent === lastPages) {
            alert('已经是最后一页了');
            return;
        }
        getData(pagesCurrent + 1);
    } else {
        if (pagesCurrent === 1) {
            alert('已经是第一页了');
            return;
        }
        getData(pagesCurrent - 1);
    }
}

function goTopage(obj) {
    getData(obj.innerHTML);
}

function checkMore(obj) {
    // console.log('被点击了', obj.id);
    location.href= "news-details.html?id=" + obj.id;
}
